﻿using System.Collections;
using UnityEngine;
using System;
using System.Threading;
using System.Linq;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.IO;
using System.Text;
using IMIForUnity;


namespace IMIForUnity
{	
	public  partial class BodyMeaMgrImp
	{

		public float hipsWidth = 0;  
		public float hipsThickness=0f;

		 float hipsRatio=1.05f;
//		public ClusterSmoothTool hipsSmthWideness;
//		public ClusterSmoothTool hipsSmthThickness;

		public ArrayList hipsSmthWidthArr=new ArrayList();
		public ArrayList hipsSmthThicknessArr=new ArrayList();

		public float hipsWidetmp=0f;
		public float hitHoriExtRatio=0.5f;
		



		void ProcessHipsSide(ref Dictionary<int, ImiPlayerInfo> playerInfos ,int userID, bool isDrawOnDepthMap)
		{
			int dw = DEPTHWIDTH;
			int dh = DEPTHHEIGHT;


			int NeckPixPosX=0;
			int NeckPixPosY=0;
			int NeckPixPosZ=0;			
			GetPlayerJointPixPos (ref  playerInfos ,userID, ref NeckPixPosX, ref NeckPixPosY, ref NeckPixPosZ, 
			                      ImiSkeleton.Index.SHOULDER_CENTER,isDrawOnDepthMap);
			int SpinePixPosX=0;
			int SpinePixPosY=0;
			int SpinePixPosZ=0;
			GetPlayerJointPixPos (ref  playerInfos ,userID, ref SpinePixPosX, ref SpinePixPosY, ref SpinePixPosZ, 
			                      ImiSkeleton.Index.SPINE,isDrawOnDepthMap);
			int hipsMidPixPosX=0;
			int hipsMidPixPosY=0;
			int hipsMidPixPosZ=0;
			GetPlayerJointPixPos (ref  playerInfos ,userID, ref hipsMidPixPosX, ref hipsMidPixPosY, ref hipsMidPixPosZ, 
			                     ImiSkeleton.Index.HIP_CENTER,false);
			int hipleftX=0;
			int hipleftY=0;
			int hipleftZ=0;
			GetPlayerJointPixPos (ref  playerInfos ,userID, ref hipleftX, ref hipleftY, ref hipleftZ, 
			                      ImiSkeleton.Index.HIP_LEFT,isDrawOnDepthMap);




			//To measure the hips thickness.
			int hipssideupx=RangeValue(hipsMidPixPosX, 0 ,dw);
			//int hipssideupy=RangeValue(hipleftY, 0 ,dh);
			int hipssideupy=RangeValue(HipsY-(int)(((float)(HipsY-NeckPixPosY))*0.1f),
			                           0 ,dh); 
			

			int hipssidedownx=RangeValue(hipsMidPixPosX, 0 ,dw);;
			//int hipssidedowny=RangeValue((hipleftY+(hipleftY-hipsMidPixPosY)),0 ,dh); 
			int hipssidedowny=RangeValue(HipsY+(int)(((float)(HipsY-NeckPixPosY))*0.4f),
			                             0 ,dh); 


			Vector2 HipsUpPixPos = new Vector2(hipssideupx,hipssideupy );
			Vector2 HipsDownPixPos = new Vector2(hipssidedownx,hipssidedowny );

						
			//we add a adjusting deeping value on the waist surface depth.
				
			//method 1 get the thicknest part of the hips side	by using 2 pixposes
			int tempPlayerIndex0 = depthPlayerDataOriginal  [hipssidedowny *dw +hipssidedownx] & 7;
			int tempPlayerIndex1 = depthPlayerDataOriginal  [hipssideupy *dw +hipssideupx] & 7;
				
				
				//Only when the key pix pos symplify an player pixel.
			if((tempPlayerIndex0>0)&&(tempPlayerIndex1>0)&&(tempPlayerIndex0== tempPlayerIndex1 ))
			{
					
					float hipsSideTmp=GetThickestPartBy2PixPoses(dw,dh,depthPlayerDataOriginal,
					                                             boundThreshval,HipsUpPixPos,HipsDownPixPos,isDrawOnDepthMap);	
								
					
				//Debug.Log("+++++++++++++++++++++++++++hips side tmep: "+hipsSideTmp.ToString());
//					hipsSmthThickness.InsertValue (hipsSideTmp);
//					hipsThickness=hipsSmthThickness.GetFilteredValue();

				hipsSmthThicknessArr.Add(hipsSideTmp);
			 }


			if(isDrawOnDepthMap)
			{ 
				DrawPointOnDepthMap(640,480,(int)HipsUpPixPos.x,(int)HipsUpPixPos.y,
				                    7,allUsersDepthData,0,255,0,255);
						

				DrawPointOnDepthMap(640,480,(int)HipsDownPixPos.x,(int)HipsDownPixPos.y,
				                    7,allUsersDepthData,0,255,0,255);


				DrawPointOnDepthMap(640,480,hipsMidPixPosX,hipsMidPixPosY,
				                    7,allUsersDepthData,0,0,0,255);
				
				
				DrawPointOnDepthMap(640,480,hipleftX,hipleftY,
				                    7,allUsersDepthData,0,0,0,255);
			}		



			//method 2  get the thicknest part of the hips side	by using 1 pixposes
//			if((hipsMidPixPosX>0)&&(hipsMidPixPosX<dw)&&
//			   (hipleftY>0)&&(hipleftY<dh))
//			{
//				//we add a adjusting deeping value on the waist surface depth.
//				
//				
//				int tempPlayerIndex = depthPlayerDataOriginal  [hipleftY *dw +hipsMidPixPosX] & 7;
//				
//				
//				//Only when the key pix pos symplify an player pixel.
//				if(tempPlayerIndex>0)
//				{
//
//					int	hipsSideDepth =((depthPlayerDataOriginal [hipleftY * dw+ hipsMidPixPosX]) >> 3)
//							                       +adjustDeepingLengthForSideHips ;
//						
//
//
//
//					   float hipsSideTmp= GetThicknessFromOnePixPos(depthPlayerDataOriginal, 
//						                                                      dw, 
//						                                                      dh, 
//					                                                          hipsMidPixPosX,
//									                                             hipleftY,
//						                                                      boundThreshval,			               
//						                                                       hipsSideDepth, 
//						                                                      isDrawOnDepthMap);
//
//						hipsSmthThickness.InsertValue (hipsSideTmp);
//						hipsThickness=hipsSmthThickness.GetFilteredValue();
//				}
//			}
		}

		//In 2.17 06.  For now we use following func locate the hori chest line.
		//We use hips center as the position of waist.
		public void ProcessHips2(ref Dictionary<int, ImiPlayerInfo> playerInfos ,	int userID, bool isDrawOrNot )
		{
			int dw = DEPTHWIDTH;
			int dh = DEPTHHEIGHT;
			
			
			
			int SpineX=0;
			int SpineY=0;
			int SpineZ=0;
			
			GetPlayerJointPixPos (ref  playerInfos ,userID, ref SpineX, ref SpineY, ref SpineZ, 
			                      ImiSkeleton.Index.SPINE,isDrawOrNot);
			
			
			SpineX=RangeValue(SpineX, 0 ,dw);;
			SpineY=RangeValue(SpineY, 0 ,dh); 
			
			int tempPlayerIndex0 = depthPlayerDataOriginal  [SpineY *dw +SpineX] & 7;
			
			
			int neckX=0;
			int neckY=0;
			int neckZ=0;
			GetPlayerJointPixPos (ref  playerInfos ,userID, ref neckX, ref neckY, ref neckZ, 
			                      ImiSkeleton.Index.SHOULDER_CENTER,isDrawOrNot);
			
			
			neckX=RangeValue(neckX, 0 ,dw);;
			neckY=RangeValue(neckY, 0 ,dh); 
			
			
			HipsY=SpineY+(int)(((float)(SpineY-neckY)));
			
			//Only when the key pix pos symplify an player pixel.
			if(tempPlayerIndex0>0)
			{				
				if( isDrawOrNot )
				{
					
					for(int i=MinContourLimitX;i<MaxContourLimitX;i++)
					{
						DrawingSpotsOnTexture.DrawPixelsSquareOnByteArray(DEPTHWIDTH,DEPTHHEIGHT,
						                                                  i,HipsY,
						                                                  3,allUsersDepthData,0,255,0,255);
					}				
				}
			}

						
			hipsFrontDepthLineMgr.ClearGraph ();
			hipsFrontDepthLineMgr.ClearSampleDatas ();
			hipsFrontDepthLineMgr.enableDrawing = true;
			hipsFrontDepthLineMgr.FillRowNormaliezedDepthDataAndFillZeroGap ( DepthPlayerDataOriginal,  MinContourLimitX,
			                                                                  MaxContourLimitX,  HipsY, 640, 480);
//			hipsFrontDepthLineMgr.DrawDepthAnalysisArr ();
//			hipsFrontDepthLineMgr.DrawGraph ();
		    LeftHipsLimit =  MinContourLimitX + hipsFrontDepthLineMgr.sharpBeginIdx + hipsFrontDepthLineMgr.armBodyGapLeftIdx;
		    RightHipsLimit =  MinContourLimitX + hipsFrontDepthLineMgr.sharpBeginIdx + hipsFrontDepthLineMgr.armBodyGapRightIdx;
			
		}
		
		
		public float GetHipsGirth2()
		{
			return IMIBodyMeaTool.GetGirth2(hipsFrontDepthLineMgr.frontLen0, hipsFrontDepthLineMgr.frontLen1,
			                 hipsFrontDepthLineMgr.frontSideChordThick,hipsThickness,remainGirthRatioHips0,remainGirthRatioHips1);			
		}


		public float  GetHipsGirth( )
		{
			return IMIBodyMeaTool.CalculateHipsGirth(hipsWidth, hipsThickness,hipsRatio);
			
		}



		public float HipsfrontArcLength()
		{
			return hipsFrontDepthLineMgr.frontLen0;
		}
		
		public float HipsfrontChordLength()
		{
			return hipsFrontDepthLineMgr.frontLen1;
		}







	}
}
